package com.hulk.dryad.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.hulk.dryad.admin.api.vo.SysPublicParamVO;
import com.hulk.dryad.admin.mapstruct.SysPublicParamStruct;
import com.hulk.dryad.common.annotation.Idempotent;
import com.hulk.dryad.common.annotation.OperLog;
import com.hulk.dryad.common.annotation.PrintlnLog;
import com.hulk.dryad.common.component.R;
import com.hulk.dryad.common.constant.enums.ActionType;
import com.hulk.dryad.common.constant.enums.BEC;
import com.hulk.dryad.persistence.entity.SysPublicParamModel;
import com.hulk.dryad.manage.provider.service.SysPublicParamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 公共参数
 *
 * @author kim
 * @date 2019-04-29
 */
@ApiSort(9)
@Api(value = "param", tags = "公共参数配置")
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/param")
public class SysPublicParamController {

	private final SysPublicParamService sysPublicParamService;

	/**
	 * 通过key查询公共参数值
	 * @param publicKey  公共参数KEY
	 * @return boolean
	 */
	@ApiOperation(value = "查询公共参数值", notes = "根据key查询公共参数值")
	@PrintlnLog
	@GetMapping("/publicValue/{publicKey}")
	public R publicKey(@PathVariable("publicKey") String publicKey) {
		return R.ok(sysPublicParamService.getSysPublicParamKeyToValue(publicKey));
	}

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param vo 实体
	 * @return Page
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@Idempotent
	@GetMapping("/page")
	public R getSysPublicParamPage(Page page, SysPublicParamVO vo) {
		SysPublicParamStruct INSTANCE =	SysPublicParamStruct.INSTANCE;
		SysPublicParamModel model = INSTANCE.to(vo);
		Page<SysPublicParamModel> rpm =	sysPublicParamService.page(page, Wrappers.query(model));
		Page<SysPublicParamVO> rpv = INSTANCE.from(rpm);
		return R.ok(rpv);
	}

	/**
	 * 通过id查询公共参数
	 * @param publicId ID
	 * @return SysPublicParamVO
	 */
	@ApiOperation(value = "通过id查询公共参数", notes = "通过id查询公共参数")
	@PrintlnLog
	@GetMapping("/{publicId}")
	public R getById(@PathVariable("publicId") Long publicId) {
		SysPublicParamModel model =  sysPublicParamService.getById(publicId);
		SysPublicParamVO vo =	SysPublicParamStruct.INSTANCE.from(model);
		return R.ok(vo);
	}

	/**
	 * 验证公共参数名称是否存在
	 * @param publicName 公共参数名
	 * @return count
	 */
	@ApiOperation(value = "checkByName", notes = "验证公共参数名称是否存在")
	@PrintlnLog
	@GetMapping("/name/{publicName}")
	public R checkByName(@PathVariable String publicName) {
		int count =sysPublicParamService.count(Wrappers.<SysPublicParamModel>lambdaQuery().eq(SysPublicParamModel::getPublicName,publicName));
		if(count>0){
			return R.failed(BEC.E_1006,count);
		}
		return R.ok(count);
	}
	/**
	 * 验证公共参数KEY是否存在
	 * @param publicKey 公共参数KEY
	 * @return count
	 */
	@ApiOperation(value = "checkByCode", notes = "验证公共参数KEY是否存在")
	@PrintlnLog
	@GetMapping("/key/{publicKey}")
	public R checkByKey(@PathVariable String publicKey) {
		int count =sysPublicParamService.count(Wrappers.<SysPublicParamModel>lambdaQuery().eq(SysPublicParamModel::getPublicKey,publicKey));
		if(count>0){
			return R.failed(BEC.E_1006,count);
		}
		return R.ok(count);
	}

	/**
	 * 新增公共参数
	 * @param vo 实体
	 * @return SysPublicParamModel
	 */
	@ApiOperation(value = "新增公共参数", notes = "新增公共参数")
	@Idempotent
	@OperLog(value ="新增公共参数",action = ActionType.ADD)
	@PostMapping
	@PreAuthorize("@pms.hasPermission('admin_syspublicparam_add')")
	public R save(@RequestBody SysPublicParamVO vo) {
		SysPublicParamModel model = 	SysPublicParamStruct.INSTANCE.to(vo);
		return R.ok(sysPublicParamService.save(model));
	}

	/**
	 * 修改公共参数
	 * @param vo 实体
	 * @return SysPublicParamModel
	 */
	@ApiOperation(value = "修改公共参数", notes = "修改公共参数")
	@Idempotent
	@OperLog(value ="修改公共参数",action = ActionType.UPDATE)
	@PutMapping
	@PreAuthorize("@pms.hasPermission('admin_syspublicparam_edit')")
	public R updateById(@RequestBody SysPublicParamVO vo) {
		SysPublicParamModel model = 	SysPublicParamStruct.INSTANCE.to(vo);
		return sysPublicParamService.updateParam(model);
	}

	/**
	 * 通过id删除公共参数
	 * @param publicId ID
	 * @return boolean
	 */
	@ApiOperation(value = "删除公共参数", notes = "删除公共参数")
	@OperLog(value ="删除公共参数",action = ActionType.DEL)
	@DeleteMapping("/{publicId}")
	@PreAuthorize("@pms.hasPermission('admin_syspublicparam_del')")
	public R removeById(@PathVariable Long publicId) {
		return sysPublicParamService.removeParam(publicId);
	}

}
